package com.meiyou.service.impl;


import com.meiyou.context.BaseContext;
import com.meiyou.dto.CollectDto;
import com.meiyou.entity.Collect;
import com.meiyou.mapper.CollectMapper;
import com.meiyou.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.service.impl
 * @Author: 尹晓安
 * @Date: 2023/11/25 18:10
 * @Description:
 */
@Service
public class CollectServiceImp implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    /**
     * 点赞、取消点赞文章
     *
     * @param collectDto
     * @return
     */
    @Override
    public void collectOrCancel(CollectDto collectDto) {

        // 1.查询数据表里是否有该数据，有则删除，无则添加（点赞和取消点赞）
        Collect collect = new Collect();

        collect.setArticleId(collectDto.getArticleId());
        collect.setUserId(BaseContext.getCurrentId());
        // 查询数据表
        Integer count = collectMapper.select(collect);
        if (count > 0) {
            // 有则删除
            collectMapper.delete(collect);
            return;
        }
        // 无则添加
        collectMapper.add(collect);

    }

    /**
     * 获取用户文章被收藏数
     *
     * @return
     */
    @Override
    public Integer getCollectNum() {

        Collect collect = new Collect();
        collect.setUserId(BaseContext.getCurrentId());
        Integer count = collectMapper.select(collect);

        return count;
    }

    /**
     * 批量删除收藏文章
     *
     * @param ids
     * @return
     */
    @Override
    public void removeBatch(List<Long> ids) {

        collectMapper.removeBatch(ids);

    }
}
